//
//  AddTicketMemberDetail.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/1/23.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "AddTicketMemberDetail.h"
#import "SelectCountryVC.h"
#import "IGDatePickerView.h"
@interface AddTicketMemberDetail ()<SelectCountryVCDelegate,IGDatePickerViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UIView *nameView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *surnameLabel;
@property (weak, nonatomic) IBOutlet UITextField *surnameTextField;
@property (weak, nonatomic) IBOutlet UIView *surnameView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *surnameViewHeight;

@property (weak, nonatomic) IBOutlet UILabel *enNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *enNameTextField;
@property (weak, nonatomic) IBOutlet UIView *enNameView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *enNameViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *regionsLabel;
@property (weak, nonatomic) IBOutlet UITextField *regionsTextField;
@property (weak, nonatomic) IBOutlet UIView *regionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *regionViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *passportLabel;
@property (weak, nonatomic) IBOutlet UITextField *passportTextField;
@property (weak, nonatomic) IBOutlet UIView *passportView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *passportViewHeight;


@property (weak, nonatomic) IBOutlet UILabel *cardIDLabel;
@property (weak, nonatomic) IBOutlet UITextField *cardIDTextField;


@property (weak, nonatomic) IBOutlet UILabel *bornDateLabel;
@property (weak, nonatomic) IBOutlet UITextField *bornDateTextField;
@property (weak, nonatomic) IBOutlet UIView *birthdayView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *birthdayViewHeight;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UIButton *completeBtn;


@property (nonatomic, strong)IGDatePickerView * datePicker;


@property (weak, nonatomic) IBOutlet UILabel *phoneNameLabel;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumTextField;

@property (weak, nonatomic) IBOutlet UIButton *countryCodeBtn;

@property (nonatomic, assign)BOOL isNeedCode;
@property (nonatomic, strong)Country *county;
@end

@implementation AddTicketMemberDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setup];
    [self setupData];
}

- (TicketMemberInfoModel *)memberModel {
    
    if (!_memberModel) {
        
        _memberModel = [[TicketMemberInfoModel alloc] init];
    }
    return _memberModel;
}

- (IGDatePickerView *)datePicker {
    
    if (!_datePicker) {
        
        _datePicker = [[IGDatePickerView alloc] init];
        _datePicker.delegate = self;
    }
    
    return _datePicker;
}
    
-(void)setup {
    
    self.topSpace.constant = kNavigationBarHeight + 10;
    self.title = MYLocalizedString(@"TICKET_USER_TITLE", nil);
    [self setNavThemeGradient];
    
//    self.nameLabel.text = MYLocalizedString(@"TICKET_USER_NAME", nil);
    self.nameLabel.textColor = kSubitleColor;
    self.nameTextField.placeholder = MYLocalizedString(@"TICKET_USER_NAME", nil);
    self.nameTextField.textColor = kContentColor;
    if(![self.mustLimitArray containsObject:@"realname"]){
        self.nameView.hidden = YES;
        self.nameViewHeight.constant = 0;
    }
    
    
//    self.surnameLabel.textColor = kSubitleColor;
//    self.surnameLabel.text = MYLocalizedString(@"TICKET_USER_PINYIN_NAME", nil);
    self.surnameTextField.textColor = kContentColor;
    self.surnameTextField.delegate = self;
    self.surnameTextField.placeholder = MYLocalizedString(@"TICKET_PASSPORT_REALNAME_P", nil);
    if(![self.mustLimitArray containsObject:@"pinyin"]){
        self.surnameView.hidden = YES;
        self.surnameViewHeight.constant = 0;
    }
    
//    self.enNameLabel.textColor = kSubitleColor;
//    self.enNameLabel.text = MYLocalizedString(@"TICKET_USER_PINYIN_SURNAME", nil);
    self.enNameTextField.placeholder = MYLocalizedString(@"TICKET_PASSPORT_LASTNAME_P", nil);
    self.enNameTextField.textColor = kContentColor;
    self.enNameTextField.delegate = self;
    if(![self.mustLimitArray containsObject:@"pinyin"]){
        self.enNameView.hidden = YES;
        self.enNameViewHeight.constant = 0;
    }
    
    
//    self.regionsLabel.textColor = kSubitleColor;
//    self.regionsLabel.text = MYLocalizedString(@"TICKET_USER_COUNTRY", nil);
    self.regionsTextField.placeholder = MYLocalizedString(@"TICKET_USER_COUNTRY", nil);
    self.regionsTextField.enabled = NO;
    self.regionsTextField.textColor = kContentColor;
    if(![self.mustLimitArray containsObject:@"passport"]){
        self.regionView.hidden = YES;
        self.regionViewHeight.constant = 0;
    }
    
   
//    self.passportLabel.textColor = kSubitleColor;
//    self.passportLabel.text = MYLocalizedString(@"TICKET_USER_PASSPORT_NUMBER", nil);
    self.passportTextField.placeholder = MYLocalizedString(@"TICKET_PASSPORT_P", nil);
    self.passportTextField.textColor = kContentColor;
    if(![self.mustLimitArray containsObject:@"passport"]){
        self.passportView.hidden = YES;
        self.passportViewHeight.constant = 0;
    }
    
    
//    self.cardIDLabel.textColor = kSubitleColor;
//    self.cardIDLabel.text = MYLocalizedString(@"TICKET_CARDID", nil);
//    self.cardIDTextField.placeholder = MYLocalizedString(@"TICKET_CARDID_P", nil);
//    if([self.mustLimitArray containsObject:@"idcode"]){
//        self.cardIDTextField.placeholder =[NSString stringWithFormat:@"%@ %@",MYLocalizedString(@"TICKET_ADD_MEMBER_REQUIRE", nil),MYLocalizedString(@"TICKET_CARDID_P", nil)] ;
//    }
//    self.cardIDTextField.textColor = kContentColor;
    
    
//    self.bornDateLabel.textColor = kSubitleColor;
//    self.bornDateLabel.text = MYLocalizedString(@"TICKET_USER_BIRTHDAY", nil);
    self.bornDateTextField.enabled = NO;
    self.bornDateTextField.placeholder = MYLocalizedString(@"TICKET_SELECT_BIRTHDAY", nil);
    self.bornDateTextField.textColor = kContentColor;
    if(![self.mustLimitArray containsObject:@"birthday"]){
        self.birthdayView.hidden = YES;
        self.birthdayViewHeight.constant = 0;
    }
    
    
//    self.phoneNameLabel.textColor = kSubitleColor;
//    self.phoneNameLabel.text = MYLocalizedString(@"TICKET_USER_PHONE", nil);
//    self.phoneNumTextField.placeholder = MYLocalizedString(@"LOGIN_MOBILE_PLACEHOLDER", nil);
//    if([self.mustLimitArray containsObject:@"telephone"]){
//        self.phoneNumTextField.placeholder =[NSString stringWithFormat:@"%@ %@",MYLocalizedString(@"TICKET_ADD_MEMBER_REQUIRE", nil),MYLocalizedString(@"LOGIN_MOBILE_PLACEHOLDER", nil)];
//    }
//    self.phoneNumTextField.textColor = kContentColor;
//    [self.countryCodeBtn setTitleColor:kContentColor forState:UIControlStateNormal];
//    [self.countryCodeBtn setTitle:MYLocalizedString(@"LOGIN_PLACE", nil) forState:UIControlStateNormal];
//    self.sexNameLabel.textColor = kSubitleColor;
//    self.sexTextField.textColor = kContentColor;
//    self.sexTextField.placeholder = MYLocalizedString(@"TICKET_SELECT_SEX_P", nil);
//    self.sexNameLabel.text = MYLocalizedString(@"TICKET_SELECT_SEX", nil);
//    if (self.isComplete) {
//
//        self.nameTextField.text = @"";
//        self.nameTextField.enabled = NO;
//
//        self.surnameTextField.text = @"";
//        self.surnameTextField.enabled = NO;
//
//        self.enNameTextField.text = @"";
//        self.enNameTextField.enabled = NO;
//
//        self.regionsTextField.text = @"";
//        self.regionsTextField.enabled = NO;
//
//        self.passportTextField.text = @"";
//        self.passportTextField.enabled = NO;
//
//        self.cardIDTextField.text = @"";
//        self.bornDateTextField.text = @"";
//    }
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goSelectRegion)];
    
    [self.regionView addGestureRecognizer:tap];
    
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goSelectDate)];
    
    [self.birthdayView addGestureRecognizer:tap2];
}

- (void)setupData{
    
    if (self.memberModel) {
        
        self.nameTextField.text = self.memberModel.realname;
        self.surnameTextField.text = self.memberModel.firstname;
        self.enNameTextField.text = self.memberModel.lastname;
        self.regionsTextField.text = self.memberModel.passportcountry;
        self.county.smartName = self.memberModel.passportcountry;
        self.passportTextField.text = self.memberModel.passportno;
        self.bornDateTextField.text = self.memberModel.birthday;
    }
}

- (IBAction)selectCountryCode:(UIButton *)sender {
    
    [self closeKeyBoard];
    SelectCountryVC * vc = [[SelectCountryVC alloc] init];
    vc.delegate = self;
    vc.isOnlyCity = YES;
    
    [vc showWithVC:self];
}


- (IBAction)completeBtnClicked:(UIButton *)sender {
    
    if ([self checkContent]) {
        
        self.memberModel.realname = self.nameTextField.text;
        self.memberModel.firstname = self.surnameTextField.text;
        self.memberModel.lastname = self.enNameTextField.text;
        self.memberModel.passportcountry = self.regionsTextField.text;
        self.memberModel.passportno = self.passportTextField.text;
        self.memberModel.birthday = self.bornDateTextField.text;
        
        [NetAPIManager addTravelerInformationWith:self.memberModel block:^(TicketMemberInfoModel *ticketDetailModel, NSError *error) {
           
            if (ticketDetailModel && !error) {
                
                [IGUtil showToast:MYLocalizedString(@"TICKET_ADDCONTACT_SUCCESS", nil)];
                if (_delegate && [_delegate respondsToSelector:@selector(refreshData)]) {
                    
                    [_delegate refreshData];
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [IGUtil showToast:MYLocalizedString(@"TICKET_ADDCONTACT_FAIL", nil)];
            }
        }];
        
    }else {
        
        [IGAlertView alertWithTitle:MYLocalizedString(@"TICKET_SELECT_RESERVATION_TIP", nil) message:MYLocalizedString(@"TICKET_SELECT_RESERVATION_INFO", nil) cancelButtonTitle:MYLocalizedString(@"TICKET_SELECT_RESERVATION_GOON", nil) commitBtn:nil commit:nil cancel:nil];
    }
}


- (BOOL)checkContent {
    
    BOOL flag = YES;
    
    for (NSString * str in self.mustLimitArray) {
        
        if ([str isEqualToString:@"realname"]) {
            
            if (![self stringIsHaveContentWith:self.nameTextField.text]) {
                
                flag = NO;
            }
        }else if([str isEqualToString:@"pinyin"]){
            
            if (![self stringIsHaveContentWith:self.enNameTextField.text]||![self stringIsHaveContentWith:self.surnameTextField.text]) {
                
                flag = NO;
            }
            
        }else if([str isEqualToString:@"passport"]){
            
            if (![self stringIsHaveContentWith:self.passportTextField.text] ||![self stringIsHaveContentWith:self.regionsTextField.text]) {
                
                flag = NO;
            }
        }else if([str isEqualToString:@"birthday"]){
            
            if (![self stringIsHaveContentWith:self.bornDateTextField.text]) {
                
                flag = NO;
            }
        }
        
    }
    
    return flag;
}

- (BOOL)stringIsHaveContentWith:(NSString *)str {
    
    NSString * content =  [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    return content.length > 0? YES:NO;
}

- (void)goSelectRegion{
    
    [self closeKeyBoard];
    SelectCountryVC * vc = [[SelectCountryVC alloc] init];
    vc.delegate = self;
    
    [vc showWithVC:self];
    
}


- (void)goSelectDate {
    
    [self closeKeyBoard];
    self.sl_popupController.layoutType = PopupLayoutTypeBottom;
    [self.sl_popupController presentContentView:self.datePicker];
    
    self.datePicker.selectedDate = self.memberModel.birthdayDate;
}


#pragma mark -- UIPickerViewDelegate

- (void)cancelSelet {
    
    [self.sl_popupController dismiss];
}

- (void)confirmTheDate:(NSDate *)date {
    
    self.memberModel.birthdayDate = date;
    self.memberModel.birthday = [date stringWithFormat:@"yyyy-MM-dd"];

    self.bornDateTextField.text = self.memberModel.birthday;
    [self.sl_popupController dismiss];
}
#pragma mark -- SelectCountryVCDelegate

- (void)selectedCountry:(Country *)country {
    
    
    self.county = country;

    self.regionsTextField.text = country.smartName;
    self.memberModel.passportcountry = country.smartName;
}


- (void)closeKeyBoard {
    [self.nameTextField endEditing: YES];
    [self.surnameTextField endEditing:YES];
    [self.enNameTextField endEditing:YES];
    [self.passportTextField endEditing:YES];
    [self.cardIDTextField endEditing:YES];
}

#pragma mark  -- UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (string.length == 0 ){return YES;}
    char commitChar = [string characterAtIndex:0];
    if (commitChar > 96 && commitChar < 123){
        NSString * uppercaseString = string.uppercaseString;
        NSString * str1 = [textField.text substringToIndex:range.location];
        NSString * str2 = [textField.text substringFromIndex:range.location];
        textField.text = [NSString stringWithFormat:@"%@%@%@",str1,uppercaseString,str2].uppercaseString;
        return NO;
    }
    return YES;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [super touchesBegan:touches withEvent:event];
    
    [self closeKeyBoard];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
